package com.cq.ssm.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

//登陆认证拦截器
public class LoginInterceptor extends HandlerInterceptorAdapter{
	//进入Handler方法之前执行
	//用于身份认证、身份授权
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		//获取请求的url
		String url = request.getRequestURI();
		//判断url是否是公开地址，实际使用时将公开地址配置到配置文件中
		if(url.indexOf("login.action")>=0){
			//如果进行登陆提交，放行
			return true;
		}
		//判断session
		HttpSession session = request.getSession();
		//从session中取出用户身份信息
		String username = (String) session.getAttribute("username");
		if(username!=null){
			//身份存在，放行
			return true;
		}
		//跳转到首页
		request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
		return false;
	}
}
